import {balanceInquiry, queryPaymentWallet, queryWallet} from '@/api/wallet'
import storage from '@/utils/storage'

export default {
  data(){
    return {
      receiver: {},
      paymentDTO: {},
      wallet:{}
    }
  },
  computed: {
    orgId () {
      // if (process.env.NODE_ENV === 'development') return storage.session.get('role') //'DATA_BUSINESS'
      let info = storage.session.get('userInfo')
      return info ? (info.deptId || '') : ''
    },
    isLoginState(){
      return storage.session.get('authToken') && storage.session.get('isLogin')
    }
  },
  mounted () {
    try{
      this.getWallet()
    }catch(e){

    }
  },
  methods: {
    /**
     * 钱包主体
     * */
    async getWallet(){
      if(!this.isLoginState)return;
      if(!this.orgId) return
      let res = await queryWallet({orgId: this.orgId});
      if(res.code === 0 && res.data.length !== 0){
        storage.session.set('wallet',res.data[0])
        this.wallet = res.data[0]
        this.getBalance()
      }
    },
    /**
     * 钱包余额
     * */
    async getBalance(accountNo){
      let wallet = storage.session.get('wallet') || {}
      if(!wallet.virtualCardNo)return;
      let res = await balanceInquiry({accountNo:wallet.virtualCardNo});
      wallet.balance = res.data || 0
      storage.session.set('wallet',wallet)
      this.wallet = wallet
    },
    /**
     * 钱包
     * */
    async queryPaymentWallet (amount,receiver) {
      if(!this.isLoginState)return;
      let wallet = storage.session.get('wallet') || {}
      if (!wallet.id) throw '当前用户还未开通钱包！'
      if (wallet.status == 0) throw '钱包开通审核中,不能交易！';
      let payer = this.orgId
      // let payer = 'DATA_BUSINESS'
      // receiver = 'DATA_REQUESTER'
      let res = await queryPaymentWallet({payer, receiver})
      if (res.code !== 0) return
      let { payList, recvList } = res.data
      if(payList.length == 0) throw '当前用户还未开通钱包！'
      if(recvList.length == 0) throw '数据提供方未开通钱包，请联系运营通知'
      if (recvList[0].status == 0) throw '数据提供方钱包开通审核中,不能交易！';
      this.receiver = recvList[0]
      let { virtualName: payAccNm, virtualCardNo: payAccNo } = payList[0]
      let { virtualName: recvAccNm, virtualCardNo: recvAccNo } = recvList[0]
      this.paymentDTO = {
        amount,
        payAccNm,
        payAccNo,
        recvAccNm,
        recvAccNo,
        type: 'payment'
      }
    },
  }
}
